package com.gitee.blogcase.gracepaycase.container.config;

import com.gitee.blogcase.gracepaycase.annocation.Pay;
import com.gitee.blogcase.gracepaycase.container.PayBeanContainer;
import com.gitee.blogcase.gracepaycase.service.PayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * 支付bean注册表
 *
 * @author wentao.wu
 * @date 2022/01/04
 */
@Slf4j
@Component
public class PayServiceInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {

    @Autowired
    private PayBeanContainer payBeanContainer;

    /**
     * 发布过程实例化后
     *
     * @param bean     豆
     * @param beanName bean的名字
     * @return boolean
     * @throws BeansException 豆子例外
     */
    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        if (bean instanceof PayService) {
            Pay annotation = bean.getClass().getAnnotation(Pay.class);
            payBeanContainer.addBean(annotation.mode(), (PayService) bean);
            log.info(" PayBeanContainer.addBean:{}", bean.getClass().getName());
        }
        return true;
    }
}
